//*************************************************************************************************
// DLLMAIN.CPP
//
// Code for handling DLL loading, unloading, COM Registration, etc.
//*************************************************************************************************
#include "stdafx.h"
#include "olehelp.h"						// OLE helper routines.
#include "ssautoi.h"						// Internal SourceSafe automation defs.
#include <winnt.h>

HINSTANCE 		ghInst = NULL;

//*************************************************************************************************
// Windows DLL entry point for notifications.
//*************************************************************************************************
extern "C" int __declspec(dllexport) WINAPI DllMain(
	HINSTANCE	hInst,
	DWORD		fdwReason,
	LPVOID		lpReserved)
{
	switch (fdwReason)
	{
		case DLL_PROCESS_ATTACH:
			ghInst = hInst;
			Trace::Write("DLL_PROCESS_ATTACH");
			break;

		case DLL_THREAD_ATTACH:
			Trace::Write("DLL_THREAD_ATTACH");
			break;
			
		case DLL_PROCESS_DETACH:
			CTypeInfo::Terminate();
			Trace::Write("DLL_PROCESS_DETACH");
			Trace::CloseFile();
			break;

		case DLL_THREAD_DETACH:
			Trace::Write("DLL_THREAD_DETACH");
			break;
		default:
			Trace::Write("DLLMain %d", fdwReason);
			break;
	}

	return TRUE;
}

// Registry strings.
static TCHAR gszRegName[]				= _T("Microsoft Visual SourceSafe Automation");
static TCHAR gszVersion[]				= _T("6.0");
static TCHAR gszSS[]					= _T("SourceSafe");
static TCHAR gszSS0[]					= _T("SourceSafe.8.0");

//*************************************************************************************************
// Register all of the classes implemented by this OLE server.
//*************************************************************************************************
STDAPI DllRegisterServer()					// S_OK or error.
{
	return E_NOTIMPL;
/*TODO:
    TCHAR       szModule[MAX_PATH+1];		// This server's module name.
	WCHAR		szModule2[MAX_PATH+1];		// Unicode file spec.
	TCHAR		szHelpPath[MAX_PATH];		// The help file path.
	WCHAR		szHelpPath2[MAX_PATH];		// Unicode file spec.
	ITypeLib	*pITypeLib;					// TypeLib object.
	HRESULT		hr;

	// Erase all doubt from old entries.
	DllUnregisterServer();

	// Get the filename for this module.
    GetModuleFileName(ghInst, szModule, sizeof(szModule)/sizeof(TCHAR));

	// Register each OLE class defined in this DLL.
	if (RegisterClass(CLSID_VSSDatabase, gszRegName, gszSS0, gszSS, szModule) < 0)
	{
		return REGDB_E_WRITEREGDB;
	}

	// Register the type library.
	MultiByteToWideChar(CP_ACP, 0, szModule, -1, szModule2, MAX_PATH+1);
	if (SUCCEEDED(hr = LoadTypeLib(szModule2, &pITypeLib)))
	{
		// The help files are in the directory with the dll.
		MSFS::GetPath(szModule, szHelpPath, NULL);
		MultiByteToWideChar(CP_ACP, 0, szHelpPath, -1, szHelpPath2, MAX_PATH);
		hr = RegisterTypeLib(pITypeLib, szModule2, szHelpPath2);
		pITypeLib->Release();
	}

	return hr;
*/
}

//*************************************************************************************************
// Unregister all of the classes implemented by this OLE server.
//*************************************************************************************************
STDAPI DllUnregisterServer()			// S_OK or error.
{
	// Register each OLE class defined in this DLL.
	if (UnregisterClass(CLSID_VSSDatabase, gszSS0, gszSS) < 0 ||
		UnregisterTypeLib(LIBID_SourceSafeTypeLib, gszVersion) < 0)
	{
		return REGDB_E_WRITEREGDB;
	}
	return S_OK;
}
